package com.xiaoyao.novel.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.xiaoyao.novel.common.Pagin;

/**
 * Ibatis Dao 基类.增删改查.分页.获取所有,删除所有.
 * 
 * @author 旷明爱
 * @date 2013-8-13 上午11:40:37
 */
@SuppressWarnings("unchecked")
public class BaseDao<T> {
	protected SqlMapClient session = IbatisUtil.getSession();
	protected String PREFIX = getClass().getName().replaceFirst(".+\\.", "") + ".";

	public void insert(T object) throws SQLException {
		session.insert(PREFIX + "insert", object);
	}

	public void insert(List<T> list) throws SQLException {
		for (T object : list) {
			insert(object);
		}
	}

	public void delete(Object id) throws SQLException {
		session.delete(PREFIX + "delete", id);
	}

	public void update(T object) throws SQLException {
		session.update(PREFIX + "update", object);
	}

	public T getById(Object id) throws SQLException {
		return (T) session.queryForObject(PREFIX + "getById", id);
	}

	// ////

	/**
	 * 返回键值对形式的数据
	 * 
	 * @param pagin
	 * @return
	 * @throws SQLException
	 */
	public List<Map<String, Object>> searchMap(Pagin pagin) throws SQLException {
		return session.queryForList(PREFIX + "searchMap", pagin);
	} 
	public List<T> search(Pagin pagin) throws SQLException {
		return session.queryForList(PREFIX + "search", pagin);
	}

	public int searchCount(Pagin pagin) throws SQLException {
		return Integer.parseInt(session.queryForObject(PREFIX + "searchCount", pagin) + "");
	}

	//
	public List<T> getAll() throws SQLException {
		return session.queryForList(PREFIX + "getAll");
	}

	public void deleteAll() throws SQLException {
		session.delete(PREFIX + "deleteAll");
	}
	 

}
